/*
 * Mp4Muxer.h
 *
 *  Created on: 2021-08-02
 *      Author: chuanjiang.zh
 */

#ifndef MP4MUXER_H_
#define MP4MUXER_H_

#include "BasicType.h"
#include "MovMemoryBuffer.h"
#include "fmp4-writer.h"

#include "UMediaFormat.h"
#include <functional>


class Mp4Muxer
{
public:
	typedef std::function< void(uint8_t* data, int size, int64_t pts, int flags) >	MuxerSink;

public:
    Mp4Muxer();
    virtual ~Mp4Muxer();

	virtual int open(const UMediaFormat& fmt);

	virtual void close();

	virtual bool isOpen();

	virtual bool write(int type, uint8_t* data, int size, int64_t pts, int64_t dts, int flags);

	virtual void setSink(MuxerSink sink);

	virtual void flush();

protected:
	void closeWriter();

	void fireMuxerData(const std::string& data, int flags);

protected:
	fmp4_writer_t*	m_writer;
	MovMemoryBuffer	m_buffer;
	MuxerSink	m_sink;
	int	m_videoTrack;
	int m_audioTrack;

	bool	m_keyWanted;
	int64_t	m_lastTs;

};

#endif /* MP4MUXER_H_ */
